package DAL;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import entities.CandidatePass;
import entities.ConnectionManager;

public class CandidatePassDA {
	public CandidatePass[] selectCandidatePasses(CandidatePass CandidatePass)
	{
		boolean whereClause = false;
		String query = "SELECT * from CandidatePass  " ;
		if(CandidatePass.getUserID() != -1)
		{
			query += "WHERE UserID = " + CandidatePass.getUserID();
			whereClause = true;
		}

		if(!CandidatePass.getPasswordHash().equals("")) 
		{
			if(whereClause)
				query += " AND passwordHash = '" + CandidatePass.getPasswordHash() + "'";
			else
				query += " WHERE passwordHash = '" + CandidatePass.getPasswordHash() + "'";
			whereClause = true;
		}
		if(!CandidatePass.getSalt().equals("")) 
		{
			if(whereClause)
				query += " AND Salt = '" + CandidatePass.getSalt() + "'";
			else
				query += " WHERE Salt = '" + CandidatePass.getSalt() + "'";
			whereClause = true;
		}

		ConnectionManager con = new ConnectionManager();
		ResultSet rs = con.executeSelectQuery(query);
		
		ArrayList<CandidatePass> CandidatePasses = new ArrayList<CandidatePass>();

		
		try {
			while(rs.next())
			{
				CandidatePass temp= new CandidatePass();
				temp.setUserID(rs.getInt("UserID"));
				temp.setPasswordHash(rs.getString("passwordHash"));
				temp.setSalt(rs.getString("salt"));
				CandidatePasses.add(temp);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		CandidatePass [] temp = new CandidatePass [0];
		return CandidatePasses.toArray(temp);

	}


	public boolean updateCandidatePass(CandidatePass CandidatePass)
	{
		String query = "UPDATE CandidatePass SET " ;
		if(!CandidatePass.getPasswordHash().equals("")) 
		{
			query += " passwordHash = '" + CandidatePass.getPasswordHash() + "'";
			if(!CandidatePass.getSalt().equals("")) 
			{
				query += " , salt = '" + CandidatePass.getSalt() + "'";
			}
		}
		else
		{
			if(!CandidatePass.getSalt().equals("")) 
			{
				query += " salt = '" + CandidatePass.getSalt() + "'";
			}
		}
		
		query += " WHERE UserID = "+ CandidatePass.getUserID() ;
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return (x==1);
	}
	
	public boolean insertCandidatePass(CandidatePass CandidatePass)
	{
		String query = "INSERT INTO CandidatePass ( UserID , PasswordHash , salt) VALUES " +
				"(" + CandidatePass.getUserID() + " ,  '"+ CandidatePass.getPasswordHash()+ "' , '" + CandidatePass.getSalt()+ "' )";
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return x==1;
	}
	
	public int deleteCandidatePass(CandidatePass CandidatePass)
	{
		boolean whereClause = false;
		String query = "DELETE FROM CandidatePass ";
		if(CandidatePass.getUserID() != -1)
		{
			query += "WHERE UserID = " + CandidatePass.getUserID();
			whereClause = true;
		}
		
		if(!CandidatePass.getPasswordHash().equals("")) 
		{
			if(whereClause)
				query += " AND passwordHash = '" + CandidatePass.getPasswordHash() + "'";
			else
				query += " WHERE passwordHash = '" + CandidatePass.getPasswordHash() + "'";
			whereClause = true;
		}
		
		if(!CandidatePass.getSalt().equals("")) 
		{
			if(whereClause)
				query += " AND salt = '" + CandidatePass.getSalt() + "'";
			else
				query += " WHERE salt = '" + CandidatePass.getSalt() + "'";
			whereClause = true;
		}
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return x;
	}
	
	
	public static void main(String[] args) {
		CandidatePass u = new CandidatePass();

		
		//u.setEmail("asdsadasd");
		//u.setPasswordHash("AdminPasddasass121asd");
		//u.setId(1);
		//u.setUserID(1121);
		//u.setPasswordHash("asdsad1111sa");
		u.setSalt("asdsad");
		CandidatePassDA da = new CandidatePassDA();
		System.out.println(da.deleteCandidatePass(u));
	}

}
